package com.zxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxy.common.ResultData;
import com.zxy.dto.DormitoryInfo;
import com.zxy.entity.Dormitory;
import com.zxy.mapper.DormitoryMapper;
import com.zxy.service.IDormitoryService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * 宿舍列表
 * </p>
 *
 * @author Zxy
 * @since 2021-05-11
 */
@RestController
@RequestMapping("/dormitory")
@AllArgsConstructor
public class DormitoryController {

    private final IDormitoryService dormitoryService;

    private final DormitoryMapper dormitoryMapper;

    // 获取全部宿舍信息
    @GetMapping("/findall")
    public ResultData allDormitory(@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {

        Page<DormitoryInfo> page = new Page<>(current, size);
        IPage<DormitoryInfo> list = dormitoryMapper.findList(page);
        return ResultData.success(list);
    }

    // 新增宿舍人数
    @PostMapping("/add")
    public ResultData addInfo(Dormitory dormitory){
        dormitoryService.save(dormitory);
        return ResultData.success("新增成功");
    }

    // 统计当前宿舍的人数
    @GetMapping("/count")
    public ResultData count(Integer id){
        QueryWrapper<Dormitory> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Integer count = dormitoryMapper.selectCount(wrapper);
        return ResultData.success(count);
    }

}
